<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue'
import loginInfoStore from '@/stores/modules/login'
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter } from 'vue-router'
import type { FormRules, FormInstance } from 'element-plus'
import setting from '@/setting'
interface RuleForm {
  username: string
  password: string
}

// 用户登录信息pinia
const userInfo = loginInfoStore()
// 加载状态
let loading = ref(false)
// 路由
const $router = useRouter()

// 表单验证规则
const rules = reactive<FormRules<RuleForm>>({
  username: [{ required: true, message: '请输入账号名称', trigger: 'change' }],
  password: [{ required: true, message: '请输入账号密码', trigger: 'change' }],
})

const ruleFormRef = ref<FormInstance>()

const loginForm = reactive({
  username: 'admin',
  password: '123456',
})

// 登录事件
const loginEvt = async () => {
  // 表单校验
  ruleFormRef.value?.validate((valid) => {
    if (valid) {
      loading.value = true

      // 1.调用pinia触发登录请求
      userInfo
        .userLogin(loginForm)
        .then(() => {
          $router.push('/')
          ElNotification({
            message: '登录成功',
            type: 'success',
          })
          loading.value = false
        })
        .catch((err) => {
          ElNotification({
            message: err,
            type: 'error',
          })
          loading.value = false
        })
    } else {
      ElNotification({
        message: '还有未填项，请检查！',
        type: 'error',
      })
    }
  })

  // 2.登录成功后跳转到首页
  // 3.登录失败提示错误信息
}
</script>
<script lang="ts">
export default {
  name: 'loginPage',
}
</script>
<template>
  <div class="login">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" ref="ruleFormRef" :model="loginForm" :rules="rules" status-icon>
          <h1>Hello</h1>
          <p>欢迎来到{{ setting.title }}后台</p>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="password"
              autocomplete="off"
              placeholder="请输入"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              :show-password="true"
              placeholder="请输入"
              :prefix-icon="Lock"
            />
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" style="width: 100%" type="primary" @click="loginEvt">
              登&nbsp;录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login {
  position: relative;
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/login_background.jpg') no-repeat center / cover;
  .login_form {
    width: 500px;
    background: url('@/assets/images/login_form.png') no-repeat center / cover;
    background-color: transparent;
    position: absolute;
    top: 40vh;
    padding: 20px;
    border-radius: 10px;
    h1 {
      color: #fff;
    }
    p {
      margin: 15px 0;
      color: #fff;
      font-size: 16px;
    }
  }
}
</style>
